<?php
class Article_linkController extends MyAdminController
{
	const PAGE_SIZE = 50;

	public function init()
	{
		parent::init();
	}

	//显示链接模型数据
	public function actionIndex()
	{
		$page = intval(Yii::app()->request->getParam('pageNum')) - 1;
		$keyword = Yii::app()->request->getParam('keyword');
		$category_id = intval(Yii::app()->request->getParam('category_id'));

		// 查库
		$CDbCriteria = new CDbCriteria;
		$CDbCriteria->order = "create_time DESC";

		// 分页
		$count = Article_link::model()->count($CDbCriteria);
		$pages = new CPagination($count);
		$pages->pageSize = self::PAGE_SIZE;
		$pages->currentPage = $page;
		$pages->applyLimit($CDbCriteria);

		$rows = Article_link::model()->findAll($CDbCriteria);

		$CDbCriteria = new CDbCriteria;
		$CDbCriteria->order = "id DESC";
		$CDbCriteria->condition = "model_type = 1";
		$categorys = Article_category::model()->findAll($CDbCriteria);

		// 显示
		$data = array();
		$data['rows'] = $rows;
		$data['pages'] = $pages;
		$data['categorys'] = $categorys;
		$this->display('index', $data);
	}

	/**
	 * 添加
	 */
	public function actionAdd()
	{
		$category_id = intval(Yii::app()->request->getParam('category_id'));
		$title = Yii::app()->request->getParam('title');
		$url = Yii::app()->request->getParam('url');
		$description = Yii::app()->request->getParam('description');
		$navTabId = Yii::app()->request->getParam('navTabId');
		$new_window = intval(Yii::app()->request->getParam('new_window'));

		$category = Article_category::model()->findAll('model_type=1');
		//如果提交内容
		if($title)
		{
			if(!empty($_FILES['photo']['name']) && $_FILES['photo']['error'] === UPLOAD_ERR_OK)
			{
				$upload = CUploadedFile::getInstanceByName('photo');
				if($upload)
				{
					$ext = explode('.', $_FILES["photo"]['name']);
					$ext = array_pop($ext);
					$md5_name = md5($_FILES["photo"]["tmp_name"]);
					$md5_path = substr($md5_name, 0, 2);
					$photo = "/data/{$md5_path}/{$md5_name}.{$ext}";
					if(!file_exists(ROOT."/../project.www.05wan.com/public/data/{$md5_path}"))
					{
						mkdir(ROOT."/../project.www.05wan.com/public/data/{$md5_path}");
					}
					$upload->saveAs(ROOT."/../project.www.05wan.com/public{$photo}");
				}
			}
			else
			{
				$photo = '';
			}

			$article_link = new Article_link();
			$article_link->title = $title;
			$article_link->category_id = $category_id;
			$article_link->url = $url;
			$article_link->photo = $photo;
			$article_link->create_time = date('Y-m-d H:i:s');
			$article_link->new_window = $new_window;
			$article_link->description = $description;
			if($article_link->save())
			{
				$this->iframe_alert_ok('操作成功', array('navTabId' => $navTabId, 'callbackType' => 'closeCurrent'));
			}
			else
			{
				$this->iframe_alert_error();
			}
		}
		else
		{
			$data = array();
			$data['category'] = $category;
			$this->display('add', $data);
		}
	}

	/**
	 * 修改
	 */
	public function actionEdit()
	{
		$id = intval(Yii::app()->request->getParam('id'));
		$category_id = intval(Yii::app()->request->getParam('category_id'));
		$title = Yii::app()->request->getParam('title');
		$url = Yii::app()->request->getParam('url');
		$description = Yii::app()->request->getParam('description');
		$photourl = trim(Yii::app()->request->getParam('photourl'));
		$new_window = intval(Yii::app()->request->getParam('new_window'));

		$category = Article_category::model()->findAll('model_type=1');
		if(!$id)
		{
			$this->iframe_alert_error('ID不能为空');
		}

		$CDbCriteria = new CDbCriteria;
		$CDbCriteria->condition = "id = '{$id}'";
		$link = Article_link::model()->find($CDbCriteria);
		if(!$link)
		{
			$this->alert_error('链接不存在');
		}

		//如果提交内容
		if($title)
		{
			$photo = '';
			if(!empty($_FILES['photo']['name']) && $_FILES['photo']['error'] === UPLOAD_ERR_OK)
			{
				$upload = CUploadedFile::getInstanceByName('photo');
				if($upload)
				{
					$ext = explode('.', $_FILES["photo"]['name']);
					$ext = array_pop($ext);
					$md5_name = md5($_FILES["photo"]["tmp_name"]);
					$md5_path = substr($md5_name, 0, 2);
					$photo = "/data/{$md5_path}/{$md5_name}.{$ext}";
					if(!file_exists(ROOT."/../project.www.05wan.com/public/data/{$md5_path}"))
					{
						mkdir(ROOT."/../project.www.05wan.com/public/data/{$md5_path}");
					}
					$upload->saveAs(ROOT."/../project.www.05wan.com/public{$photo}");
				}
			}

			$link->title = $title;
			$link->category_id = $category_id;
			$link->url = $url;
			$link->new_window = $new_window;
			$link->description = $description;
			
			if($photo)
			{
				$link->photo = $photo;
			}
			else
			{
				$link->photo = $photourl;
			}
			
			if($link->save())
			{
				$this->iframe_alert_ok('操作成功');
			}
			else
			{
				$this->iframe_alert_error();
			}
		}
		else
		{
			

			$data = array();
			$data['link'] = $link;
			$data['category'] = $category;
			$this->display('edit', $data);
		}
	}

	/**
	 * 删除链接
	 */
	public function actionDelete()
	{
		$id = intval(Yii::app()->request->getParam('id'));
		if(!$id)
		{
			$this->alert_error('ID不能为空');
		}
		$article = Article_link::model()->findByPk($id);
		if(!$article)
		{
			$this->alert_error('链接不存在。');
		}

		if($article->delete())
		{
			$this->alert_ok();
		}
		else
		{
			$this->alert_error();
		}
	}
}